<template>
  <!-- 新建歌单 -->
  <div>
    <!-- 新建歌单 -->
    <el-button @click="openDialog()">新建歌单</el-button>
    <!-- 点击新建歌单弹出抽屉 -->
    <el-drawer v-model="createDialog" title="新建歌单" direction="rtl" class="demo-drawer" size="30%">
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-form-item>
            <el-input ref="songListNameRef" v-model="songListName" autocomplete="off" clearable placeholder="请输入新歌单标题" />
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm">取消</el-button>
          <el-button type="danger" @click="playlistCreate(songListName)">创建</el-button>
        </div>
      </div>
    </el-drawer>
  </div>

  <!-- 编辑歌单 -->
  <el-drawer v-model="drawer" :with-header="false" size="30%">
    <h3 class="edit-title">编辑歌单</h3>
    <div class="edit-table">
      <section>
        当前歌单名称：<input type="text" ref="getname" :value="songmsg.name">
      </section>
      <section class="section-sec">
        当前歌单描述：
        <!-- :value="songmsg.description" -->
        <textarea type="text" :value="songmsg.description" ref="getdesc" maxlength="100" class="class-desc" />
        <span>100</span>
      </section>
      <section>
        当前歌单标签：
        <input type="text" ref="gettags" :value="songmsg.tags">
      </section>
    </div>
    <!-- 歌单标签 -->
    <main class="tags-box">
      <span v-for="tag in tags" @click="getTagName(tag.name)">{{ tag.name }}</span>
    </main>
    <div class="demo-drawer__footer edit-btnbox">
      <el-button @click="drawer = false">取消</el-button>
      <!-- playlistUpdate(id, sname, desc, tags) -->
      <el-button type="danger" @click="getData()">更新</el-button>
    </div>
  </el-drawer>

  <!-- 获取用户歌单列表 -->
  <ul class="listul">
    <li :class="{ bgChange: songlist.subscribed }" :key="songlist.coverImgUrl" v-for="songlist in songlists">
      <img :src="songlist.coverImgUrl" alt="" class="listpic">
      <section>
        <div class="listtitle">
          <el-icon class="titleicon">
            <Service />
          </el-icon>
          <h3>
            {{ songlist.name }}
            <span v-if="songlist.subscribed">
              <el-icon class="star">
                <StarFilled />
              </el-icon>
            </span>
          </h3>
        </div>
        <div class="listdetail">
          <p>歌曲数量：{{ songlist.trackCount }}</p>
          <p>播放次数：{{ songlist.playCount }}</p>
          <p>创建时间：{{ songListCreateTime(songlist.createTime) }}</p>
          <p>歌单属性：{{ songlist.subscribed === true ? '收藏歌单' : '个人歌单' }}</p>
        </div>
      </section>
      <section class="btns">
        <button style="cursor: pointer;" @click="drawer = true; getSongInfo(songlist)">编辑歌单</button>
        <button style="cursor: pointer;" @click="playlistDelete(songlist.id)">删除歌单</button>
      </section>
    </li>
  </ul>
</template>
<script>
import { mapState } from 'vuex'
import { Service, StarFilled } from '@element-plus/icons-vue'
// 精品歌单标签列接口
import { getHighqualityTags } from '../../api/topic'
// 用户歌单列表接口
import { getUserSongsListInterface } from '../../api/user'
// 新建歌单接口，删除歌单接口，更新歌单接口
import { playlistCreateInterface, playlistDeleteInterface, playlistUpdateInterface } from '@/api/artist'

export default {
  created() {
    // 将当前用户id作为参数传入接口中获取当前登录用户的歌单列表
    this.getSongsList({ uid: this.userInfo.userId })
  },
  data() {
    return {
      createDialog: false,
      songListName: '',
      form: {},
      // 歌单列表 
      songlists: [],
      // 歌单id
      songListId: '',
      drawer: false,
      // 当前要修改的歌单信息
      songmsg: '',
      // 当前歌单描述的字符数量
      textVal: 0,
      textareaData: '',
      // 歌单标签
      tags: [],
      // 标签传参格式
      tab: ''
    }
  },
  computed: {
    ...mapState('userInfoModule', ['userInfo']),
  },

  methods: {
    // 点击新建歌单弹出输入歌单名
    openDialog() {
      this.createDialog = true;
      setTimeout(() => {
        this.$refs.songListNameRef.focus()
      })
    },

    // 清除输入框输入的新建歌单名内容
    clearInputName() {
      this.songListName = ''
    },

    // 点击创建
    playlistCreate(name) {
      playlistCreateInterface({ name }).then(res => {
        // console.log(res)
        // console.log(this.songListName)
        // 若创建成功显示创建成功
        if (res.code === 200) {
          ElMessage({
            message: '创建成功',
            type: 'success',
          })
        }
      })
      this.createDialog = false
      this.clearInputName();
      this.getSongsList({ uid: this.userInfo.userId })
    },

    // 点击取消,关闭drawer
    cancelForm() {
      this.createDialog = false;
      this.clearInputName();
    },

    // 获取用户歌单列表
    getSongsList(params) {
      getUserSongsListInterface(params).then(res => {
        this.songlists = res?.playlist
        // trackCount 轨迹计数，歌曲数量
        // playCount 播放次数
        // console.log('songlists', this.songlists);
      })
    },

    // 歌单创建时间换算
    songListCreateTime(createtime) {
      const date = new Date(createtime);
      return `${date.getFullYear()} 年 ${date.getMonth()} 月 ${date.getDate()} 日 ${date.getHours()}：${date.getMinutes()}：${date.getSeconds()}`;
    },

    // 抽屉中获取歌单信息
    getSongInfo(info) {
      // info.id 歌单id
      // info.name 歌单名字
      // info.description 歌单描述
      // info.tags 歌单标签
      this.songmsg = info
      // 获取歌词标签方法执行
      // console.log(this.songmsg.tags, '12456547579');
      this.getSongTags()
    },

    // 获取歌词标签
    getSongTags() {
      getHighqualityTags().then(res => {
        this.tags = res.tags
      })
    },


    // 获取输入框数据
    getData() {
      this.songmsg.name = this.$refs.getname.value
      this.songmsg.description = this.$refs.getdesc.value
      // this.songmsg.tags = this.$refs.gettags.value
      this.$refs.gettags.value = this.tabStr

      console.log(this.songmsg.tags, '获取输入框数据this.tag');

      // 编辑歌单方法执行
      this.playlistUpdate(this.songmsg.id, this.songmsg.name, this.songmsg.description, this.tabStr)

      this.drawer = false
    },

    // 编辑歌单
    playlistUpdate(id, sname, desc, tags) {
      // console.log(info, 'info');
      playlistUpdateInterface({
        id, name: sname, desc, tags
      }).then(res => {
        if (res.code === 200) {
          ElMessage({
            message: '编辑成功',
            type: 'success',
          })
        }
      })
    },

    // 获取当前点击标签名
    getTagName(tagname) {
      // console.log(tagname);
      this.tabname = tagname

      this.tabStr = this.songmsg.tags.join(';')
      if (this.tabStr.includes(tagname)) {
        this.songmsg.tags = this.songmsg.tags.filter(item => {
          return item !== tagname
        })
        return
      }
      this.tabStr += `;${tagname}`
      this.songmsg.tags.push(tagname)

      console.log(this.songmsg.tags, 'this.songmsg.tags');
      console.log(this.tabStr, 'this.tabStr');
    },

    // 删除歌单
    playlistDelete(id) {
      playlistDeleteInterface({ id }).then(res => {
        // console.log('playlistDelete', res)
        // console.log(id)
        // 若删除成功显示删除成功
        if (res.code === 200) {
          ElMessage({
            message: '删除成功',
            type: 'success',
          })
        }
      })
      this.getSongsList({ uid: this.userInfo.userId })
    },
  },
  components: {
    Service,
    StarFilled
  }
}
</script>

<style lang="scss" scoped>
.listul {
  padding: 0 20px;
}

.listul>li {
  list-style: none;
  width: 100%;
  height: 150px;
  background-color: rgb(212, 205, 202);
  margin: 15px 0;
  border-radius: 10px;
  display: flex;
  align-items: center;
}

.listul>li:nth-of-type(1) {
  background-color: rgb(156, 162, 162);
}

// 表单标题
.listtitle {
  display: flex;
  align-items: center;
  width: 100%;
  height: 50px;

  h3 {
    text-indent: 5px;
    font-size: 18px;
    color: rgb(55, 55, 54);
    margin-bottom: 15px;
  }

  .titleicon {
    font-size: 16px;
    margin-bottom: 10px;
  }
}

// 收藏歌单星星样式
.star {
  color: rgb(231, 231, 33);
  font-size: 25px;
}

// 表单详情
.listdetail {
  text-indent: 5px;
  font-size: 12px;
  color: rgb(109, 111, 112);
  width: 300px;
}

// 图片
.listpic {
  width: 110px;
  height: 110px;
  border-radius: 5px;
  margin: 0 30px;
}

// 按钮样式
.btns {
  width: 40%;
  display: flex;
  justify-content: end;
}

button {
  width: 90px;
  height: 40px;
  margin: 0 5px;
  border: 1px solid #b39f9f;
  border-radius: 5px;
  color: #666;
  font-size: 14px;
}

button:hover {
  color: rgb(146, 18, 18);
}

.bgChange {
  background-color: rgba(254, 184, 184, 0.8) !important;
}

// 编辑歌单
.edit-title {
  text-align: center;
  font-size: 28px;
  margin-bottom: 20px;
  color: #ccc1c1;
}

.edit-table {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 40%;
}

.edit-table>section {
  width: 90%;
  margin: 20px;
  font-size: 18px;
  color: #666;

  input {
    width: 60%;
    height: 100%;
    border-radius: 5px;
    border: 1px solid #666;
    text-indent: 5px;
    color: #393737;
    outline: none;
  }
}

// 歌单标签
.tags-box {
  margin-top: 50px;
  height: 100px;
  overflow: auto;
  border-radius: 10px;
  padding: 10px;
  color: #393737;


  span {
    width: 60px;
    height: 40px;
    background-color: #f5eeee;
    border-radius: 5px;
    text-align: center;
    line-height: 40px;
    margin: 2px;
    display: inline-block;
  }
}

// 当前歌单描述
.section-sec {
  display: flex;
  align-items: center;
  position: relative;


  textarea {
    resize: none;
    border-radius: 10px;
    padding: 5px;
    outline: none;
    outline-color: #666;
    width: 60%;
    color: #393737;
  }

  textarea:focus {
    box-shadow: inset 0px 0px 5px 2px rgb(155, 153, 153);
  }

  span {
    font-size: 12px;
    position: absolute;
    right: 30px;
    bottom: 5px;
    color: #a29494;
  }
}

.class-desc {
  height: 80px;
}

.edit-btnbox {
  margin-top: 30px;
  display: flex;
  justify-content: center;

  button {
    margin: 0 20px;
  }
}

// 新建歌单抽屉按钮样式
.demo-drawer__footer {
  .el-button:nth-of-type(1):hover {
    color: #fff;
    background-color: RGB(248, 152, 152);
  }

  .el-button:nth-of-type(2) {
    color: #fff;
  }

}
</style>